﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using OS.BLL;

namespace DownloadPlug_ins
{
    public partial class Form1 : Form
    {
        public Common2 common = new Common2();
        public static string url = "";



        #region 页面属性
        public string Dmbh
        {
            get;
            set;
        }
        #endregion

        public Form1()
        {
            InitializeComponent();
            ConfigModel model = common.GetXml();//获取相关配置属性
            url = model.WebService;
        }

        private void DoWork(int type)
        {
            bool status = false;
            if (this.chkDryymxb.Checked)
                status = true;
            if (this.chkXsrmx.Checked)
                status = true;
            if (this.chkCkd.Checked)
                status = true;
            if (this.chkRkd.Checked)
                status = true;

            if (this.chkDcd.Checked)
                status = true;
            if (this.chkPdb.Checked)
                status = true;
            if (this.chkKczb.Checked)
                status = true;
            if (this.chkKhyszz.Checked)
                status = true;



            if (!status)
            {
                MessageBox.Show("请勾选需要上传的内容！");
            }
            else
            {
                this.osProgress1.AutoProgress = true;
                ThreadPool.QueueUserWorkItem(new WaitCallback(this.OptData), type);
            }


        }
        private void OptData(object o)
        {
            if (this.chkDryymxb.Checked)
            {
                UpLoadDryymxb();
            }
            if (this.chkXsrmx.Checked)
            {
                UpLoadXsrmx();
            }
            if (this.chkCkd.Checked)
            {
                UpLoadCkd();
            }
            if (this.chkRkd.Checked)
            {
                UpLoadRkd();
            }

            if (this.chkDcd.Checked)
            {
                UpLoadDcd();
            }
            if (this.chkPdb.Checked)
            {
                UpLoadPdb();
            }
            if (this.chkKczb.Checked)
            {
                UpLoadKczb();
            }
            if (this.chkKhyszz.Checked)
            {
                UpLoadKhyszz();
            }
            this.osProgress1.AutoProgress = false;
        }
        private void DownloadWork(int type)
        {
            bool status = false;
            if (this.chkSpda.Checked)
                status = true;
            if (this.chkDbd.Checked)
                status = true;

            if (!status)
            {
                MessageBox.Show("请勾选需要下载的内容！");
            }
            else
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(this.DownloadData), type);
                this.osProgress2.AutoProgress = true;
            }
        }


        /// <summary>
        /// 上传 当日营业明细表
        /// </summary>
        private void UpLoadDryymxb()
        {
            Bll_zwcl_dryymxbH bllzwcl_dryymxbH = new Bll_zwcl_dryymxbH();
            // int Count = basicBLL.GetxskdHCount(" where  djstate=0");//待上传销售单记录数
            int Size = 20;//一次提取数量
            int pageIndex = 1;

            int count = 0;
            while (true)
            {
                int startIndex = (pageIndex - 1) * Size + 1;
                int endIndex = pageIndex * Size;
                List<zwcl_dryymxbH_Entity> ls = bllzwcl_dryymxbH.GetModelList("", "", startIndex, endIndex);

                if (ls != null && ls.Count > 0)
                {

                    JsonConvert<zwcl_dryymxbH_Entity> jc = new JsonConvert<zwcl_dryymxbH_Entity>();
                    StringBuilder sBuilder = jc.ToDataGrid(ls);


                    ArrayList parms = new ArrayList();
                    parms.Add(sBuilder.ToString());

                    string reslut = DSWebService.InvokeWebService(url, "UploadDryymxbbList", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        //修改本地数据库 xskdh状态
                        // int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {
                        MessageBox.Show(" 无法连接目标服务器请检查网络！ ");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("连接成功，但 数据上传失败请联系管理员！ ");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }

            MessageBox.Show(" 上传完成 总共记录数目：" + count);
        }

        /// <summary>
        ///上传 前台销售明细 
        /// </summary>
        private void UpLoadXsrmx()
        {
            Bll_zwcl_xsrmxH bllzwcl_xsrmxH = new Bll_zwcl_xsrmxH();
            // int Count = basicBLL.GetxskdHCount(" where  djstate=0");//待上传销售单记录数
            int Size = 20;//一次提取数量
            int pageIndex = 1;

            int count = 0;
            while (true)
            {
                int startIndex = (pageIndex - 1) * Size + 1;
                int endIndex = pageIndex * Size;
                List<zwcl_xsrmxH_Entity> ls = bllzwcl_xsrmxH.GetModelList("", "", startIndex, endIndex);

                if (ls != null && ls.Count > 0)
                {

                    JsonConvert<zwcl_xsrmxH_Entity> jc = new JsonConvert<zwcl_xsrmxH_Entity>();
                    StringBuilder sBuilder = jc.ToDataGrid(ls);


                    ArrayList parms = new ArrayList();
                    parms.Add(sBuilder.ToString());

                    string reslut = DSWebService.InvokeWebService(url, "UploadXsrmxhList", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        //修改本地数据库 xskdh状态
                        // int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {
                        MessageBox.Show(" 无法连接目标服务器请检查网络！ ");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("连接成功，但 数据上传失败请联系管理员！ ");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }

            MessageBox.Show(" 上传完成 总共记录数目：" + count);
        }


        /// <summary>
        ///5、上传前台出库明细  
        /// </summary>
        private void UpLoadCkd()
        {
            Bll_yw_ckdH bllyw_ckdH = new Bll_yw_ckdH();
            Bll_yw_ckdB bllyw_ckdB = new Bll_yw_ckdB();


            int Size = 20;//一次提取数量
            int pageIndex = 1;

            int count = 0;
            while (true)
            {
                int startIndex = (pageIndex - 1) * Size + 1;
                int endIndex = pageIndex * Size;
                List<yw_ckdH_Entity> ls = bllyw_ckdH.GetModelList("", "", startIndex, endIndex);

                if (ls != null && ls.Count > 0)
                {
                    List<yw_ckdHAll_Entity> nls = new List<yw_ckdHAll_Entity>();

                    foreach (yw_ckdH_Entity entity in ls)
                    {
                        yw_ckdHAll_Entity model = new yw_ckdHAll_Entity();

                        string strWhere1 = "djlsh=" + entity.DjLsh;
                        List<yw_ckdB_Entity> cls = bllyw_ckdB.GetModelList(strWhere1);


                        model.DjLsh = entity.DjLsh;

                        model.DjBtZdh = entity.DjBtZdh;

                        model.DjFtZdh = entity.DjFtZdh;

                        model.DjStZdh = entity.DjStZdh;

                        model.DjState = entity.DjState;

                        model.DjCount = entity.DjCount;

                        model.ckrq = entity.ckrq;

                        model.djzt = entity.djzt;
                        model.ckdh = entity.ckdh;
                        model.ckbmh = entity.ckbmh;
                        model.ckbm = entity.ckbm;
                        model.ckbh = entity.ckbh;
                        model.ckmc = entity.ckmc;
                        model.bz = entity.bz;

                        model.slhj = entity.slhj;

                        model.jehj = entity.jehj;

                        model.lsjjehj = entity.lsjjehj;

                        model.zdr = entity.zdr;

                        model.zdsj = entity.zdsj;

                        model.shr = entity.shr;
                        model.shrj = entity.shrj;

                        model.childs = cls;

                        nls.Add(model);
                    }
                    JsonConvert<yw_ckdHAll_Entity> jc = new JsonConvert<yw_ckdHAll_Entity>();
                    StringBuilder sBuilder = jc.ToDataGrid(nls);


                    ArrayList parms = new ArrayList();
                    parms.Add(sBuilder.ToString());

                    string reslut = DSWebService.InvokeWebService(url, "UploadCkdList", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        //修改本地数据库 xskdh状态
                        // int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {
                        MessageBox.Show(" 无法连接目标服务器请检查网络！ ");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("连接成功，但 数据上传失败请联系管理员！ ");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }

            MessageBox.Show(" 上传完成 总共记录数目：" + count);
        }

        /// <summary>
        /// 6、上传前台入库明细 
        /// </summary>
        private void UpLoadRkd()
        {
            Bll_yw_rkdH bllyw_rkdH = new Bll_yw_rkdH();
            Bll_yw_rkdB bllyw_rkdB = new Bll_yw_rkdB();


            int Size = 20;//一次提取数量
            int pageIndex = 1;

            int count = 0;
            while (true)
            {
                int startIndex = (pageIndex - 1) * Size + 1;
                int endIndex = pageIndex * Size;
                List<yw_rkdH_Entity> ls = bllyw_rkdH.GetModelList("", "", startIndex, endIndex);

                if (ls != null && ls.Count > 0)
                {
                    List<yw_rkdHAll_Entity> nls = new List<yw_rkdHAll_Entity>();

                    foreach (yw_rkdH_Entity entity in ls)
                    {
                        yw_rkdHAll_Entity model = new yw_rkdHAll_Entity();

                        string strWhere1 = "djlsh=" + entity.DjLsh;
                        List<yw_rkdB_Entity> cls = bllyw_rkdB.GetModelList(strWhere1);

                        model.DjLsh = entity.DjLsh;

                        model.DjBtZdh = entity.DjBtZdh;

                        model.DjFtZdh = entity.DjFtZdh;

                        model.DjStZdh = entity.DjStZdh;

                        model.DjState = entity.DjState;

                        model.DjCount = entity.DjCount;

                        model.rkrq = entity.rkrq;

                        model.djzt = entity.djzt;
                        model.rkdh = entity.rkdh;
                        model.rkbmh = entity.rkbmh;
                        model.rkbm = entity.rkbm;
                        model.ckbh = entity.ckbh;
                        model.ckmc = entity.ckmc;
                        model.gysbh = entity.gysbh;
                        model.gysmc = entity.gysmc;
                        model.rklx = entity.rklx;
                        model.bz = entity.bz;

                        model.slhj = entity.slhj;

                        model.jehj = entity.jehj;

                        model.xj = entity.xj;

                        model.qk = entity.qk;

                        model.zr = entity.zr;

                        model.yfk = entity.yfk;

                        model.wfk = entity.wfk;

                        model.cgdh = entity.cgdh;
                        model.zdr = entity.zdr;

                        model.zdsj = entity.zdsj;

                        model.shr = entity.shr;

                        model.shsj = entity.shsj;

                        model.gyszk = entity.gyszk;

                        model.ddh = entity.ddh;

                        model.lsjehj = entity.lsjehj;

                        model.khmc = entity.khmc;

                        model.childs = cls;
                        nls.Add(model);
                    }
                    JsonConvert<yw_rkdHAll_Entity> jc = new JsonConvert<yw_rkdHAll_Entity>();
                    StringBuilder sBuilder = jc.ToDataGrid(nls);


                    ArrayList parms = new ArrayList();
                    parms.Add(sBuilder.ToString());

                    string reslut = DSWebService.InvokeWebService(url, "UploadRkdList", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        //修改本地数据库 xskdh状态
                        // int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {
                        MessageBox.Show(" 无法连接目标服务器请检查网络！ ");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("连接成功，但 数据上传失败请联系管理员！ ");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }

            MessageBox.Show(" 上传完成 总共记录数目：" + count);
        }


        /// <summary>
        /// 7、上传前台调拨明细  
        /// </summary>
        private void UpLoadDcd()
        {
            Bll_yw_dcdH bllyw_dcdH = new Bll_yw_dcdH();
            Bll_yw_dcdB bllyw_dcdB = new Bll_yw_dcdB();

            int Size = 20;//一次提取数量
            int pageIndex = 1;

            int count = 0;
            while (true)
            {
                int startIndex = (pageIndex - 1) * Size + 1;
                int endIndex = pageIndex * Size;
                List<yw_dcdH_Entity> ls = bllyw_dcdH.GetModelList("", "", startIndex, endIndex);

                if (ls != null && ls.Count > 0)
                {
                    List<yw_dcdHAll_Entity> nls = new List<yw_dcdHAll_Entity>();

                    foreach (yw_dcdH_Entity entity in ls)
                    {
                        yw_dcdHAll_Entity model = new yw_dcdHAll_Entity();

                        string strWhere1 = "djlsh=" + entity.DjLsh;
                        List<yw_dcdB_Entity> cls = bllyw_dcdB.GetModelList(strWhere1);
                        model.DjLsh = entity.DjLsh;

                        model.DjBtZdh = entity.DjBtZdh;

                        model.DjFtZdh = entity.DjFtZdh;

                        model.DjStZdh = entity.DjStZdh;

                        model.DjState = entity.DjState;

                        model.DjCount = entity.DjCount;

                        model.dbrq = entity.dbrq;

                        model.djzt = entity.djzt;
                        model.dcdh = entity.dcdh;
                        model.dcbmmc = entity.dcbmmc;
                        model.dcbmh = entity.dcbmh;
                        model.drbmmc = entity.drbmmc;
                        model.drbmh = entity.drbmh;
                        model.drckbh = entity.drckbh;
                        model.drckmc = entity.drckmc;
                        model.dcckbh = entity.dcckbh;
                        model.dcckmc = entity.dcckmc;
                        model.bz = entity.bz;

                        model.slhj = entity.slhj;

                        model.jehj = entity.jehj;

                        model.jjjehj = entity.jjjehj;

                        model.zdr = entity.zdr;

                        model.zdsj = entity.zdsj;

                        model.shr = entity.shr;

                        model.shsj = entity.shsj;

                        model.childs = cls;
                        nls.Add(model);
                    }
                    JsonConvert<yw_dcdHAll_Entity> jc = new JsonConvert<yw_dcdHAll_Entity>();
                    StringBuilder sBuilder = jc.ToDataGrid(nls);


                    ArrayList parms = new ArrayList();
                    parms.Add(sBuilder.ToString());

                    string reslut = DSWebService.InvokeWebService(url, "UploadDcdList", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        //修改本地数据库 xskdh状态
                        // int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {
                        MessageBox.Show(" 无法连接目标服务器请检查网络！ ");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("连接成功，但 数据上传失败请联系管理员！ ");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }
            //  this.osProgress1.AutoProgress = false;
            MessageBox.Show(" 上传完成 总共记录数目：" + count);
        }

        /// <summary>
        /// 8、上传前台盘点明细   
        /// </summary>
        private void UpLoadPdb()
        {
            Bll_yw_pdbH bllyw_dcdH = new Bll_yw_pdbH();
            Bll_yw_pdbB bllyw_dcdB = new Bll_yw_pdbB();

            int Size = 20;//一次提取数量
            int pageIndex = 1;

            int count = 0;
            while (true)
            {
                int startIndex = (pageIndex - 1) * Size + 1;
                int endIndex = pageIndex * Size;
                List<yw_pdbH_Entity> ls = bllyw_dcdH.GetModelList("", "", startIndex, endIndex);

                if (ls != null && ls.Count > 0)
                {
                    List<yw_pdbHAll_Entity> nls = new List<yw_pdbHAll_Entity>();

                    foreach (yw_pdbH_Entity entity in ls)
                    {
                        yw_pdbHAll_Entity model = new yw_pdbHAll_Entity();

                        string strWhere1 = "djlsh=" + entity.DjLsh;
                        List<yw_pdbB_Entity> cls = bllyw_dcdB.GetModelList(strWhere1);

                        model.DjLsh = entity.DjLsh;

                        model.DjBtZdh = entity.DjBtZdh;

                        model.DjFtZdh = entity.DjFtZdh;

                        model.DjStZdh = entity.DjStZdh;

                        model.DjState = entity.DjState;

                        model.DjCount = entity.DjCount;

                        model.pdrq = entity.pdrq;


                        model.pdsj = entity.pdsj;

                        model.djzt = entity.djzt;
                        model.pddh = entity.pddh;
                        model.bmmc = entity.bmmc;
                        model.bmh = entity.bmh;
                        model.ckbh = entity.ckbh;
                        model.ckmc = entity.ckmc;
                        model.pdlb = entity.pdlb;
                        model.gysmc = entity.gysmc;
                        model.pdfs = entity.pdfs;

                        model.pdslhj = entity.pdslhj;

                        model.pdjehj = entity.pdjehj;

                        model.pdjjjehe = entity.pdjjjehe;

                        model.zmslhj = entity.zmslhj;

                        model.zmjehj = entity.zmjehj;

                        model.zmjjjehe = entity.zmjjjehe;

                        model.ykslhj = entity.ykslhj;

                        model.ykjehj = entity.ykjehj;

                        model.ykjjjehe = entity.ykjjjehe;

                        model.bz = entity.bz;
                        model.zdr = entity.zdr;

                        model.zdsj = entity.zdsj;

                        model.shr = entity.shr;

                        model.shsj = entity.shsj;

                        model.sjfs = entity.sjfs;

                        model.childs = cls;
                        nls.Add(model);
                    }
                    JsonConvert<yw_pdbHAll_Entity> jc = new JsonConvert<yw_pdbHAll_Entity>();
                    StringBuilder sBuilder = jc.ToDataGrid(nls);


                    ArrayList parms = new ArrayList();
                    parms.Add(sBuilder.ToString());

                    string reslut = DSWebService.InvokeWebService(url, "UploadPdbList", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        //修改本地数据库 xskdh状态
                        // int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {
                        MessageBox.Show(" 无法连接目标服务器请检查网络！ ");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("连接成功，但 数据上传失败请联系管理员！ ");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }
            //  this.osProgress1.AutoProgress = false;
            MessageBox.Show(" 上传完成 总共记录数目：" + count);
        }


        /// <summary>
        /// 9、上传前台库存  
        /// </summary>
        private void UpLoadKczb()
        {
            Bll_zwcl_kczbH bllyw_kczbH = new Bll_zwcl_kczbH();

            int Size = 20;//一次提取数量
            int pageIndex = 1;

            int count = 0;
            while (true)
            {
                int startIndex = (pageIndex - 1) * Size + 1;
                int endIndex = pageIndex * Size;
                List<zwcl_kczbH_Entity> ls = bllyw_kczbH.GetModelList("", "", startIndex, endIndex);

                if (ls != null && ls.Count > 0)
                {

                    JsonConvert<zwcl_kczbH_Entity> jc = new JsonConvert<zwcl_kczbH_Entity>();
                    StringBuilder sBuilder = jc.ToDataGrid(ls);


                    ArrayList parms = new ArrayList();
                    parms.Add(sBuilder.ToString());

                    string reslut = DSWebService.InvokeWebService(url, "UploadKczbList", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        //修改本地数据库 xskdh状态
                        // int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {
                        MessageBox.Show(" 无法连接目标服务器请检查网络！ ");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("连接成功，但 数据上传失败请联系管理员！ ");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }

            MessageBox.Show(" 上传完成 总共记录数目：" + count);
        }

        /// <summary>
        /// 10、上传前台客户应收总账
        /// </summary>
        private void UpLoadKhyszz()
        {
            Bll_zwcl_khyszzH bllyw_khyszzH = new Bll_zwcl_khyszzH();
            int Size = 20;//一次提取数量
            int pageIndex = 1;

            int count = 0;
            while (true)
            {
                int startIndex = (pageIndex - 1) * Size + 1;
                int endIndex = pageIndex * Size;
                List<zwcl_khyszzH_Entity> ls = bllyw_khyszzH.GetModelList("", "", startIndex, endIndex);

                if (ls != null && ls.Count > 0)
                {

                    JsonConvert<zwcl_khyszzH_Entity> jc = new JsonConvert<zwcl_khyszzH_Entity>();
                    StringBuilder sBuilder = jc.ToDataGrid(ls);


                    ArrayList parms = new ArrayList();
                    parms.Add(sBuilder.ToString());

                    string reslut = DSWebService.InvokeWebService(url, "UploadKhyszzList", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        //修改本地数据库 xskdh状态
                        // int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {
                        MessageBox.Show(" 无法连接目标服务器请检查网络！ ");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("连接成功，但 数据上传失败请联系管理员！ ");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }

            MessageBox.Show(" 上传完成 总共记录数目：" + count);
        }





        private void DownloadData(object o)
        {
            int type = (int)o;

            if (this.chkSpda.Checked)
            {
                DownloadGetCpdaH();
            }
            if (this.chkDbd.Checked)
            {
                DownloadGetDcd();
            }
            this.osProgress2.AutoProgress = false;
        }

        /// <summary>
        /// 下载商品档案 2015-09-20
        /// </summary>
        private void DownloadGetCpdaH()
        {
            ArrayList parms = new ArrayList();
            Bll_jbxx_spdaH blljbxx_spdaH = new Bll_jbxx_spdaH();

            string reslut = "";
            string strWhere = "";
            // string strWhere2 = "";
            if (radioButton3.Checked)
            {
                string startTime = this.dateTimePicker4.Value.ToString("yyyy-MM-dd") + " 00:00:00";
                string endTime = this.dateTimePicker3.Value.ToString("yyyy-MM-dd") + " 23:59:59";

                strWhere = "(( xgsj>='" + startTime + "' and xgsj<='" + endTime + "') or ( djsj>='" + startTime + "' and djsj<='" + endTime + "'))";

            }

            int count = 0;
            int pageIndex = 1;
            int pageSize = 50;
            string orderBy = "";
            while (true)
            {
                parms.Clear();
                parms.Add(strWhere);
                parms.Add(orderBy);
                parms.Add(pageSize);
                parms.Add(pageIndex);

                reslut = DSWebService.InvokeWebService(url, "DownloadSpdaList", parms.ToArray());
                if (reslut != "-2" && reslut != "[]")
                {
                    pageIndex++;
                    JsonConvert<jbxx_spdaH_Entity> jc = new JsonConvert<jbxx_spdaH_Entity>();

                    List<jbxx_spdaH_Entity> ls = jc.GetList(reslut);


                    if (ls != null && ls.Count > 0)
                    {
                        count += ls.Count;

                        //插入数据库
                        foreach (jbxx_spdaH_Entity entity in ls)
                        {
                            try
                            {
                                //if (!blljbxx_spdaH.IsExistSpbh(entity.spbh))
                                //{
                                //    bool s1 = blljbxx_spdaH.Add(entity);
                                //}
                                //else
                                //{
                                //    bool s2 = blljbxx_spdaH.Update(entity);
                                //}
                            }
                            catch (Exception ex)
                            {
                                string msg = ex.Message;

                                LogHelper.m_CreateErrorLogTxt("DownloadGetCpdaH", "商品编号：" + entity.spbh, "错误信息：" + msg);
                            }
                        }
                    }
                }
                else
                {
                    break;
                }
            }
            if (count > 0)
            {
                // this.osProgress2.AutoProgress = false;
                MessageBox.Show(" 下载完成！ 共计下载数据" + count + "条");
            }
            else
            {
                //  this.osProgress2.AutoProgress = false;
                MessageBox.Show(" 本时间段未获取到数据，可能网络原因，可能本时间段无更新数据！ ");
            }
        }

        /// <summary>
        ///下载调拨单  2015-09-20
        /// </summary>
        private void DownloadGetDcd()
        {
            ArrayList parms = new ArrayList();
            Bll_yw_dcdH bllyw_dcdH = new Bll_yw_dcdH();
            Bll_yw_dcdB bllyw_dcdB = new Bll_yw_dcdB();

            string reslut = "";
            string strWhere = "";
            // string strWhere2 = ""; ,
            if (radioButton3.Checked)
            {
                string startTime = this.dateTimePicker4.Value.ToString("yyyy-MM-dd") + " 00:00:00";
                string endTime = this.dateTimePicker3.Value.ToString("yyyy-MM-dd") + " 23:59:59";


                strWhere = "(( zdsj>='" + startTime + "' and zdsj<='" + endTime + "') or ( shsj>='" + startTime + "' and shsj<='" + endTime + "'))";

            }

            int count = 0;
            int pageIndex = 1;
            int pageSize = 50;
            string orderBy = "";
            while (true)
            {
                parms.Clear();
                parms.Add(strWhere);
                parms.Add(orderBy);
                parms.Add(pageSize);
                parms.Add(pageIndex);

                reslut = DSWebService.InvokeWebService(url, "DownloadDcdList", parms.ToArray());
                if (reslut != "-2" && reslut != "[]")
                {
                    pageIndex++;
                    JsonConvert<yw_dcdHAll_Entity> jc = new JsonConvert<yw_dcdHAll_Entity>();

                    List<yw_dcdHAll_Entity> ls = jc.GetList(reslut);


                    if (ls != null && ls.Count > 0)
                    {
                        count += ls.Count;

                        //插入数据库
                        foreach (yw_dcdHAll_Entity entity in ls)
                        {
                            try
                            {
                                yw_dcdH_Entity model = Getyw_dcdHModel(entity);

                                if (!bllyw_dcdH.Exists(entity.DjLsh))
                                {

                                    bool s1 = bllyw_dcdH.Add(model);
                                    if (s1)
                                    {
                                        List<yw_dcdB_Entity> nls = (List<yw_dcdB_Entity>)entity.childs;
                                        if (nls != null && nls.Count > 0)
                                        {
                                            bllyw_dcdB.Delete(entity.DjLsh);//先将该单据号的全部删除 然后 再行添加
                                            foreach (yw_dcdB_Entity model2 in nls)
                                            {
                                                bllyw_dcdB.Add(model2);
                                            }
                                        }

                                    }
                                }
                                else
                                {
                                    bool s2 = bllyw_dcdH.Update(model);
                                    if (s2)
                                    {
                                        List<yw_dcdB_Entity> nls = (List<yw_dcdB_Entity>)entity.childs;
                                        if (nls != null && nls.Count > 0)
                                        {
                                            bllyw_dcdB.Delete(entity.DjLsh);//先将该单据号的全部删除 然后 再行添加
                                            foreach (yw_dcdB_Entity model2 in nls)
                                            {
                                                bllyw_dcdB.Add(model2);
                                            }
                                        }

                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                string msg = ex.Message;

                                LogHelper.m_CreateErrorLogTxt("DownloadGetCpdaH", "调拨单号：" + entity.dcdh, "错误信息：" + msg);
                            }

                        }
                    }


                }
                else
                {
                    break;
                }
            }
            if (count > 0)
            {

                MessageBox.Show(" 下载完成！ 共计下载数据" + count + "条");
            }
            else
            {


                MessageBox.Show(" 本时间段未获取到数据，可能网络原因，可能本时间段无更新数据！ ");
            }



        }


        private yw_dcdH_Entity Getyw_dcdHModel(yw_dcdHAll_Entity entity)
        {
            yw_dcdH_Entity model = new yw_dcdH_Entity();
            model.DjLsh = entity.DjLsh;
            model.DjBtZdh = entity.DjBtZdh;
            model.DjFtZdh = entity.DjFtZdh;
            model.DjStZdh = entity.DjStZdh;
            model.DjState = entity.DjState;
            model.DjCount = entity.DjCount;
            model.dbrq = entity.dbrq;
            model.djzt = entity.djzt;
            model.dcdh = entity.dcdh;
            model.dcbmmc = entity.dcbmmc;
            model.dcbmh = entity.dcbmh;
            model.drbmmc = entity.drbmmc;
            model.drbmh = entity.drbmh;
            model.drckbh = entity.drckbh;
            model.drckmc = entity.drckmc;
            model.dcckbh = entity.dcckbh;
            model.dcckmc = entity.dcckmc;
            model.bz = entity.bz;
            model.slhj = entity.slhj;
            model.jehj = entity.jehj;
            model.jjjehj = entity.jjjehj;
            model.zdr = entity.zdr;
            model.zdsj = entity.zdsj;
            model.shr = entity.shr;
            model.shsj = entity.shsj;
            return model;
        }




        private void gbtnUpload_Click(object sender, EventArgs e)
        {
            DoWork(0);
        }

        private void gbtnSPDownLoad_Click(object sender, EventArgs e)
        {
            DownloadWork(0);
        }



    }
}
